﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace MyUtilityApp
{
    public partial class CallWeaterhPage : PhoneApplicationPage
    {
        public CallWeaterhPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            WebClient web = new WebClient();
            String url = String.Format("http://i.wxbug.net/REST/Direct/GetForecast.ashx?la={0}&lo={1}2&nf=7&ih=0&ht=i&l=th&c=TH&units=1&api_key=mxf782bm8qdy2chx25sn6ej2",13.9037,100.5291);
            web.DownloadStringAsync(new Uri(url));
            web.DownloadStringCompleted += web_DownloadStringCompleted;
        }

        void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //txtOutPut.Text = e.Result;

            RootObject json = JsonConvert.DeserializeObject<RootObject>(e.Result);
            weatherList.ItemsSource = json.forecastList;

            //foreach (var item in json.forecastList)
            //{
            //    txtOutPut.Text = txtOutPut.Text+ item.title;
            //    txtOutPut.Text = txtOutPut.Text + ": Low/High = "+ item.low +"/"+ item.high;
            //    txtOutPut.Text = txtOutPut.Text + "\n\n";
            //}
        }
    }

    public class ForecastList
    {
        public string dateTime { get; set; }
        public string dayDesc { get; set; }
        public string dayIcon { get; set; }
        public string dayPred { get; set; }
        public string dayTitle { get; set; }
        public bool hasDay { get; set; }
        public bool hasNight { get; set; }
        public string high { get; set; }
        public string hourly { get; set; }
        public string low { get; set; }
        public string nightDesc { get; set; }
        public string nightIcon { get; set; }
        public string nightPred { get; set; }
        public string nightTitle { get; set; }
        public string title { get; set; }
    }

    public class RootObject
    {
        public List<ForecastList> forecastList { get; set; }
        public List<int> notifications { get; set; }
        public string temperatureUnits { get; set; }
    }

}